package com.example.hxpc.guidancemanagement.fragment;


import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.ViewFlipper;

import com.example.hxpc.guidancemanagement.R;
import com.example.hxpc.guidancemanagement.adapter.MyListViewAdapter;
import com.example.hxpc.guidancemanagement.base.HonorInfo;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by tao on 2017/11/29.
 * 主页面-荣誉版Frament
 */

public class MainHonFrament extends Fragment implements View.OnClickListener {
    private View rootView ,bg_view_1 , bg_view_2;
    private TextView te_good_project , te_good_people;
    private RelativeLayout rela_good_project , rela_good_people;
    private ListView listView;
    private List<HonorInfo> mList = new ArrayList<>();
    private ViewFlipper viewFlipper;
    private int[] images = new int[]{R.drawable.we,R.drawable.metting,R.drawable.reward};
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        if (rootView == null){
            rootView = inflater.inflate(R.layout.frament_main_honor,null);
        }
        initView(rootView);
        initData();
        return rootView;
    }

    //添加图片
    public void addImgPhoto(){
        for (int i = 0 ;i <images.length ; i++){
            ImageView imageView = new ImageView(getActivity());
            imageView.setBackgroundResource(images[i]);
            viewFlipper.addView(imageView);
        }
    }

    //初始化控件
    public void initView(View rootView){
        rela_good_project = (RelativeLayout) rootView.findViewById(R.id.honor_rela_1);
        rela_good_project.setOnClickListener(this);
        rela_good_people = (RelativeLayout) rootView.findViewById(R.id.honor_rela_2);
        rela_good_people.setOnClickListener(this);
        te_good_project = (TextView) rootView.findViewById(R.id.text_good_project);
        te_good_people = (TextView) rootView.findViewById(R.id.text_good_people);
        bg_view_1 = rootView.findViewById(R.id.view_1);
        bg_view_2 = rootView.findViewById(R.id.view_2);
        listView = (ListView) rootView.findViewById(R.id.list);
        viewFlipper = (ViewFlipper) rootView.findViewById(R.id.viewflipper);
        addImgPhoto();
        viewFlipper.setAutoStart(true);
        viewFlipper.setFlipInterval(3000);
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
    }

    @Override
    public void onDestroy() {
        te_good_project.setTextColor(this.getResources().getColor(R.color.blue));
        bg_view_1.setBackgroundResource(R.color.blue);
        te_good_people.setTextColor(this.getResources().getColor(R.color.gray));
        bg_view_2.setBackgroundResource(R.color.gray);
        super.onDestroy();
    }

    //初始化数据
    public void initData(){
        mList.clear();
        int i = 0;
            while (i<3){
                HonorInfo info = new HonorInfo("项目"+i,"详细信息"+i,R.drawable.logo);
                mList.add(info);
                i++;
            }
        listView.setAdapter(new MyListViewAdapter(getActivity(),mList ));
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.honor_rela_1:
                te_good_project.setTextColor(this.getResources().getColor(R.color.blue));
                bg_view_1.setBackgroundResource(R.color.blue);
                te_good_people.setTextColor(this.getResources().getColor(R.color.gray));
                bg_view_2.setBackgroundResource(R.color.gray);
                mList.clear();
                int i = 0;
                while (i<3){
                    HonorInfo info = new HonorInfo("项目"+i,"详细信息"+i,R.drawable.logo);
                    mList.add(info);
                    i++;
                }
                listView.setAdapter(new MyListViewAdapter(getActivity(),mList));
                break;
            case R.id.honor_rela_2:
                te_good_project.setTextColor(this.getResources().getColor(R.color.gray));
                bg_view_1.setBackgroundResource(R.color.gray);
                te_good_people.setTextColor(this.getResources().getColor(R.color.blue));
                bg_view_2.setBackgroundResource(R.color.blue);
                mList.clear();
                int j = 0;
                while (j<4){
                    HonorInfo info = new HonorInfo("人物"+j,"详细信息"+j,R.drawable.logo);
                    mList.add(info);
                    j++;
                }
                listView.setAdapter(new MyListViewAdapter(getActivity(),mList));
                break;
        }
    }
}
